package jmu.edu.yjc.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jmu.edu.yjc.Utils.RedisCache;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Dictionary;
import jmu.edu.yjc.domain.entity.Elderly;
import jmu.edu.yjc.domain.entity.Keywords;
import jmu.edu.yjc.domain.entity.UnAnsweredQuestion;
import jmu.edu.yjc.domain.vo.keyword.DeleteVo;
import jmu.edu.yjc.service.DictionaryService;
import jmu.edu.yjc.service.KeywordsService;
import jmu.edu.yjc.service.UnAnsweredQuestionService;
import jmu.edu.yjc.service.impl.KeywordsServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author yjc
 * @date 2023/3/16 23:34
 */
@RestController
@RequestMapping("/knowledge")
public class KeywordController {
    @Resource
    private UnAnsweredQuestionService unAnsweredQuestionService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private KeywordsService keywordsService;
    @Resource
    private RedisCache redisCache;
    @GetMapping("/unAnswer/list")
    public ResponseResult listUnAnswers(Integer pageNum, Integer pageSize,
                                        String startTime, String endTime,
                                        String processStatus){
        return unAnsweredQuestionService.unAnswerList(pageNum,pageSize,startTime,endTime,processStatus);
    }
    @GetMapping("/keyword/list")
    public ResponseResult listKeyword(Integer pageNum, Integer pageSize ,String wordType , String keyword){
        return keywordsService.keywordList(pageNum,pageSize , wordType , keyword);
    }
    @GetMapping("/keyword/wordType")
    public ResponseResult listWordType(){
        List<String> wordTypes = keywordsService.getAllWordType();
        return ResponseResult.okResult(wordTypes);
    }
    @GetMapping("/keyword/{id}")
    public ResponseResult getKeyword(@PathVariable("id")Long id){
        Keywords keywords = keywordsService.getById(id);
        return ResponseResult.okResult(keywords);
    }
    @PutMapping("/keyword")
    public ResponseResult updateKeyword(@RequestBody Keywords keywords){
        if(keywordsService.isExistKeyword(keywords.getKeyword(),keywords.getWordType())){
            return ResponseResult.errorResult(-1,"关键字已存在");
        }
        return keywordsService.updateKeywords(keywords);
    }

    @PostMapping("/keyword")
    public ResponseResult addKeyword(@RequestBody Keywords keywords){
        if(keywordsService.isExistKeyword(keywords.getKeyword(),keywords.getWordType())){
            return ResponseResult.errorResult(-1,"关键字已存在");
        }else {
            Dictionary dictionary = dictionaryService.getByKeyAndTable(keywords.getWordType(), "t_keywords");
            keywords.setWordMean(dictionary.getKeyMean());
            return keywordsService.addUpdateWordType(keywords);
        }
    }
    @PostMapping("/keyword/delete")
    public ResponseResult delElderly(@RequestBody DeleteVo deleteVo){
        return keywordsService.deleteKeywords(deleteVo);
    }
    @GetMapping("/keyword/randomTag")
    public ResponseResult randomTags(){
        return keywordsService.randomTags();
    }
    @GetMapping("/keyword/fullTag")
    public ResponseResult fullTag(){
        return keywordsService.fullTag();
    }

    @PutMapping("/keyword/updateCache")
    public ResponseResult updateCache(){
        return keywordsService.updateRedisCache();
    }

    @PutMapping("/unAnswer/updateProcessStatus/{id}")
    public ResponseResult updateProcessStatus(@PathVariable("id") String id){
        Object[] ids = id.split(",");
        LambdaUpdateWrapper<UnAnsweredQuestion> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(UnAnsweredQuestion::getId,ids).set(UnAnsweredQuestion::getProcessStatus,"1");
        boolean update = unAnsweredQuestionService.update(wrapper);
        if(update){
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(-1,"更新失败");
        }
    }
}
